<template>
  <div class="search">
    <van-nav-bar fixed title="商品列表" left-arrow @click-left="$router.go(-1)" />

    <van-search
      readonly
      shape="round"
      background="#ffffff"
      :value="search || '搜索商品'"
      show-action
      @click="$router.push('/search')"
    >
      <template #action>
        <van-icon class="tool" name="apps-o" />
      </template>
    </van-search>

    <!-- 排序选项按钮 -->
    <div class="sort-btns">
      <div class="sort-item">综合</div>
      <div class="sort-item">销量</div>
      <div class="sort-item">价格 </div>
    </div>

    <div class="goods-list">
      <GoodsItem v-for="item in searchList" :key="item.goods_id" :item="item"></GoodsItem>
    </div>
  </div>
</template>

<script>
import GoodsItem from '@/components/GoodsItem.vue'
import router from '@/router'
import { getSearchList } from '@/api/product'

export default {
  name: 'SearchIndex',
  data () {
    return {
      page: 1,
      searchList: [],
      categoryId: null,
      search: null
    }
  },
  methods: {
    router () {
      return router
    },
    getQueryParams () {
      // 获取URL中的查询参数
      const { categoryId, search } = this.$route.query
      // 将获取到的参数赋值给组件的数据属性
      this.categoryId = categoryId
      this.search = search
    }
  },
  components: {
    GoodsItem
  },
  // computed: {
  //   querySearch () {
  //     return this.$route.query.search
  //   }
  // },
  async created () {
    const { data: { list } } = await getSearchList({
      categoryId: this.$route.query.categoryId,
      goodsName: this.categoryId,
      page: this.page
    })
    this.searchList = list.data
    // 在组件创建时获取URL参数
    this.getQueryParams()
  }
}
</script>

<style lang="less" scoped>
.search {
  padding-top: 46px;
  ::v-deep .van-icon-arrow-left {
    color: #333;
  }
  .tool {
    font-size: 24px;
    height: 40px;
    line-height: 40px;
  }

  .sort-btns {
    display: flex;
    height: 36px;
    line-height: 36px;
    .sort-item {
      text-align: center;
      flex: 1;
      font-size: 16px;
    }
  }
}

// 商品样式
.goods-list {
  background-color: #f6f6f6;
}
</style>
